/****************************************************/
*Fairness: Analysis									*/
*Date Created: February 13, 2022					*/
*Notes: Fairness Data Analysis - Figures for paper	*/
/****************************************************/

* This do file contains the figures of fairness
************************************************************************
*MAIN FIGURES:
***1a. Buyer Offer by Treatment and Value (Value==70) - LAST 10 ROUNDS
***1b. Buyer Offer by Treatment and Value (Value==90) - LAST 10 ROUNDS
***2a. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (BK v CI | Cost=10)
***2b. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (BK v CI | Cost=30)
***2c. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (NK v SK)
***3a. Seller Beliefs v. Reality (NK)
***3b. Seller Beliefs v. Reality (BK)

*APPENDIX B FIGURES:
***B2a. Buyer Offer by Treatment and Value (Value==70) - ALL 30 ROUNDS
***B2b. Buyer Offer by Treatment and Value (Value==90) - ALL 30 ROUNDS
***B3a. Buyer Offer for SK vs CI (conditional on cost for CI) - V=70
***B3b. Buyer Offer for SK vs CI (conditional on cost for CI) - V=90
***B4. CDF of Offers Used to Elicit Beliefs
***B5. Seller Acceptance Rate for BK vs NK conditional on seller's cost - ALL 30 ROUNDS
***B6. Seller Acceptance Rate for BK vs NK conditional on seller's cost - LAST ROUNDS

*************************************************************************

clear all
set more off

/****************************************/
/*				PREAMBLE	 			*/
/****************************************/

*Set directories: Change local directory paths
global raw_dir "<INSERT RAW DATA DIRECTORY HERE>"
global out_dir_gph "<INSERT OUTPUT DIRECTORY FOR FIGRUES HERE>"
global out_dir_tbl "<INSERT OUTPUT DIRECTORY FOR TABLES HERE>"
cd "$raw_dir"


*****************************************
*			MAIN FIGURES				*
*****************************************

****************************************
***1a. Buyer Offer by Treatment and Value (Value==70) - LAST 10 ROUNDS
****************************************
use fairness_part1, replace
keep if buyer==1
keep if period>20
egen numeric_id=group(subj_id)	

gen treatment_id=treatment

preserve
*save means with SE cluster
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=70
tempfile temp1    /* create a temporary file */
save "`temp1'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster for NK 
replace treatment_id=5 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==4
replace parmseq=1 if parmseq==4
gen value=70
tempfile temp2    /* create a temporary file */
save "`temp2'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=90
tempfile temp3    /* create a temporary file */
save "`temp3'"      /* save memory into the temporary file */	

restore
*save means with SE cluster for NK 
replace treatment_id=5 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==4
replace parmseq=1 if parmseq==4
gen value=90
tempfile temp4    /* create a temporary file */
save "`temp4'"      /* save memory into the temporary file */	

append using "`temp1'" 
append using "`temp2'" 
append using "`temp3'" 

sort value parmseq 

*rename variables
rename estimate mean_offer
rename stderr se_offer

gen hi_offer = mean_offer + se_offer
gen lo_offer = mean_offer - se_offer
	
rename parmseq position_xaxis1

*create graph: v=70 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==4 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==4 & value==70, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "NK" 2 "SK" 3 "BK" 4 "CI", labsize(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	text(-7 1.5 "Buyer doesn't ", size(medium)) ///
	text(-9.25 1.5 "know seller's cost", size(medium)) ///
	text(-7 3.5 "Buyer knows", size(medium)) ///	
	text(-9.25 3.5 "seller's cost", size(medium)) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerbytreatvalue70_last10only.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerbytreatvalue70_last10only.tif", replace


*create graph: v=90 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==4 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==4 & value==90, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "NK" 2 "SK" 3 "BK" 4 "CI", labsize(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	text(-7 1.5 "Buyer doesn't ", size(medium)) ///
	text(-9.25 1.5 "know seller's cost", size(medium)) ///
	text(-7 3.5 "Buyer knows", size(medium)) ///	
	text(-9.25 3.5 "seller's cost", size(medium)) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerbytreatvalue90_last10only.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerbytreatvalue90_last10only.tif", replace

****************************************
***2. Buyer's Belief and Strategic Behavior:
****************************************
****************************************
***2a. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (BK v CI | Cost=10)
****************************************
use fairness_part2, replace
keep if buyer==1

sort subj_id

gen probvalue=probBDM 

local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond"
graph twoway (lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & oldcost==10 & treatment==3 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & oldcost==10 & treatment==3 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & oldcost==10 & treatment==4 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lpattern(dash) lwidth(thick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & oldcost==10 & treatment==4 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lpattern(dash) lwidth(thick)), ///
	ylabel(, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Pr(Accept Offer)", size(large)) ///
	xtitle("Offer", size(large)) ///
	xlabel(20(5)60, labsize(medium)) ///
	ylabel(0(20)100, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///	
	legend(label(1 "BK, Value=70 & Cost=10") label(2 "BK, Value=90 & Cost=10") label(3 "CI, Value=70 & Cost=10") label(4 "CI, Value=90 & Cost=10") ///
			order(1 2 3 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 

graph export "$out_dir_gph/Fairness_beliefacceptoffer_cost10_BKvCI.png", width(3900) replace	
graph export "$out_dir_gph/Fairness_beliefacceptoffer_cost10_BKvCI.tif", replace

****KS Test
ksmirnov probvalue if buyer==1 & oldcost==10 & treatment==3, by(oldvalue) exact
ksmirnov probvalue if buyer==1 & oldcost==10 & treatment==4, by(oldvalue) exact

****************************************
***2b. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (BK v CI | Cost=30)
****************************************
use fairness_part2, replace
keep if buyer==1

sort subj_id

gen probvalue=probBDM

local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond"
graph twoway (lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & oldcost==30 & treatment==3 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & oldcost==30 & treatment==3 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & oldcost==30 & treatment==4 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lpattern(dash) lwidth(thick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & oldcost==30 & treatment==4 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lpattern(dash) lwidth(thick)), ///
	ylabel(, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Pr(Accept Offer)", size(large)) ///
	xtitle("Offer", size(large)) ///
	xlabel(20(5)60, labsize(medium)) ///
	ylabel(0(20)100, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///	
	legend(label(1 "BK, Value=70 & Cost=30") label(2 "BK, Value=90 & Cost=30") label(3 "CI, Value=70 & Cost=30") label(4 "CI, Value=90 & Cost=30") ///
			order(1 2 3 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 

graph export "$out_dir_gph/Fairness_beliefacceptoffer_cost30_BKvCI.png", width(3900) replace	
graph export "$out_dir_gph/Fairness_beliefacceptoffer_cost30_BKvCI.tif", replace


****KS Test
ksmirnov probvalue if buyer==1 & oldcost==30 & treatment==3 & oldoffer>=40 & oldoffer<=45, by(oldvalue) exact
ksmirnov probvalue if buyer==1 & oldcost==30 & treatment==4 & oldoffer>=40 & oldoffer<=45, by(oldvalue) exact

****************************************
***2c. Buyer's Belief and Strategic Behavior: Buyer Belief Offer was Accepted (NK v SK)
****************************************
use fairness_part2, replace
keep if buyer==1

sort subj_id

gen probvalue=probBDM
				 
local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond"
graph twoway (lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==70 & treatment==2 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lpattern(dash) lwidth(thick)) ///
	(lpoly probvalue oldoffer if buyer==1 & oldvalue==90 & treatment==2 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lpattern(dash) lwidth(thick)), ///
	ylabel(, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Pr(Accept Offer)", size(large)) ///
	xtitle("Offer", size(large)) ///
	xlabel(20(5)60, labsize(medium)) ///
	ylabel(0(20)100, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///	
	legend(label(1 "NK, Value=70") label(2 "NK, Value=90") label(3 "SK, Value=70") label(4 "SK, Value=90") ///
			order(1 2 3 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 

graph export "$out_dir_gph/Fairness_beliefacceptoffer_NKvSK.png", width(3900) replace
graph export "$out_dir_gph/Fairness_beliefacceptoffer_NKvSK.tif", replace


****************************************
***3a. Seller Beliefs v. Reality (NK)
****************************************
use fairness_part2, replace

bys treatment oldvalue: summarize oldoffer, detail
bys treatment oldvalue oldcost: summarize oldoffer, detail

sort subj_id

gen probvalue=probBDM/100
gen probactualvalue=.
	replace probactualvalue=1 if oldvalue==70
	replace probactualvalue=0 if oldvalue==90	 
		 
				 
local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond"
graph twoway (lpoly probvalue oldoffer if oldcost==10 & buyer==0 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if oldcost==30 & buyer==0 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lwidth(medthick)) ///
	(lpoly probactualvalue oldoffer if oldcost==10 & buyer==0 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(gs8) lpattern(dash) lwidth(thick)) ///
	(lpoly probactualvalue oldoffer if oldcost==30 & buyer==0 & treatment==1 & oldoffer>=20 & oldoffer<=60, bw(5) kernel(epanechnikov) degree(0) color(black) lpattern(dash) lwidth(thick)), ///
	ylabel(, format(%9.2f) angle(black) labsize(medium)) ///
	ytitle("Pr(Buyer Value=70)", size(large)) ///
	xtitle("Offer", size(large)) ///
	xlabel(20(5)55, labsize(medium)) ///
	ylabel(0(0.2)1, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///	
	legend(label(1 "Belief, Cost=10") label(2 "Belief, Cost=30") label(3 "Actual, Cost=10") label(4 "Actual, Cost=30") ///
			order(1 2 3 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 

graph export "$out_dir_gph/Fairness_beliefbuyervalueNK.png", width(3900) replace
graph export "$out_dir_gph/Fairness_beliefbuyervalueNK.tif", replace

****KS TEST: 
	preserve
	*keep relevant variables
	keep if treatment==1 & buyer==0
	keep subj_id period probvalue probactualvalue oldoffer oldcost buyer treatment
	*double data 
	expand 2 

	*gen indicator for actual prob
	bys subj_id period: gen n=_n
	
	gen actual=0
		replace actual=1 if n==2
		
	*gen one variable for probvalue and probactualvalue
	gen beliefactualprob=probvalue
		replace beliefactualprob=probactualvalue if actual==1

	ksmirnov beliefactualprob if oldcost==10, by(actual) exact
	ksmirnov beliefactualprob if oldcost==30, by(actual) exact		
		
	ksmirnov beliefactualprob if oldcost==10 & oldoffer>=20 & oldoffer<=60 , by(actual) exact
	ksmirnov beliefactualprob if oldcost==30 & oldoffer>=20 & oldoffer<=60 , by(actual) exact

*****Regression Test

	egen numeric_id=group(subj_id)	
	reg beliefactualprob actual oldoffer if oldcost==10, robust cluster(numeric_id)
	reg beliefactualprob actual oldoffer if oldcost==30, robust cluster(numeric_id)
	reg beliefactualprob actual i.oldoffer#i.oldcost, robust cluster(numeric_id)

	restore

****************************************
***3b. Seller Beliefs v. Reality (BK)
****************************************

local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond"
graph twoway (lpoly probvalue oldoffer if oldcost==10 & buyer==0 & treatment==3 & oldoffer>=20 & oldoffer<=55, bw(5) kernel(epanechnikov) degree(0) color(gs8) lwidth(medthick)) ///
	(lpoly probvalue oldoffer if oldcost==30 & buyer==0 & treatment==3 & oldoffer>=20 & oldoffer<=55, bw(5) kernel(epanechnikov) degree(0) color(black) lwidth(medthick)) ///
	(lpoly probactualvalue oldoffer if oldcost==10 & buyer==0 & treatment==3 & oldoffer>=20 & oldoffer<=55, bw(5) kernel(epanechnikov) degree(0) color(gs8) lpattern(dash) lwidth(thick)) ///
	(lpoly probactualvalue oldoffer if oldcost==30 & buyer==0 & treatment==3 & oldoffer>=20 & oldoffer<=55, bw(5) kernel(epanechnikov) degree(0) color(black) lpattern(dash) lwidth(thick)), ///
	ylabel(, format(%9.2f) angle(black) labsize(medium)) ///
	ytitle("Pr(Buyer Value=70)", size(large)) ///
	xtitle("Offer", size(large)) ///
	xlabel(20(5)55, labsize(medium)) ///
	ylabel(0(0.2)0.8, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///	
	legend(label(1 "Belief, Cost=10") label(2 "Belief, Cost=30") label(3 "Actual, Cost=10") label(4 "Actual, Cost=30") ///
			order(1 2 3 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 

graph export "$out_dir_gph/Fairness_beliefbuyervalueBK.png", width(3900) replace
graph export "$out_dir_gph/Fairness_beliefbuyervalueBK.tif", replace

****KS TEST: 
	preserve
	*keep relevant variables
	keep if treatment==3 & buyer==0
	keep subj_id period probvalue probactualvalue oldoffer oldcost buyer treatment
	*double data 
	expand 2 

	*gen indicator for actual prob
	bys subj_id period: gen n=_n
	
	gen actual=0
		replace actual=1 if n==2
		
	*gen one variable for probvalue and probactualvalue
	gen beliefactualprob=probvalue
		replace beliefactualprob=probactualvalue if actual==1

	ksmirnov beliefactualprob if oldcost==10, by(actual) exact
	ksmirnov beliefactualprob if oldcost==30, by(actual) exact		
		
	ksmirnov beliefactualprob if oldcost==10 & oldoffer>=20 & oldoffer<=55 , by(actual) exact
	ksmirnov beliefactualprob if oldcost==30 & oldoffer>=20 & oldoffer<=55 , by(actual) exact

*****Regression Test

	egen numeric_id=group(subj_id)	
	reg beliefactualprob actual oldoffer if oldcost==10, robust cluster(numeric_id)
	reg beliefactualprob actual oldoffer if oldcost==30, robust cluster(numeric_id)
	reg beliefactualprob actual i.oldoffer#i.oldcost, robust cluster(numeric_id)

restore 





*****************************************
*		APPENDIX FIGURES				*
*****************************************

****************************************
***B2a. Buyer Offer by Treatment and Value (Value==70) - ALL 30 ROUNDS
***B2b. Buyer Offer by Treatment and Value (Value==90) - ALL 30 ROUNDS
****************************************
use fairness_part1, replace
keep if buyer==1
egen numeric_id=group(subj_id)	

gen treatment_id=treatment

preserve
*save means with SE cluster
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=70
tempfile temp1    /* create a temporary file */
save "`temp1'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster for NK 
replace treatment_id=5 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==4
replace parmseq=1 if parmseq==4
gen value=70
tempfile temp2    /* create a temporary file */
save "`temp2'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=90
tempfile temp3    /* create a temporary file */
save "`temp3'"      /* save memory into the temporary file */	

restore
*save means with SE cluster for NK 
replace treatment_id=5 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==4
replace parmseq=1 if parmseq==4
gen value=90
tempfile temp4    /* create a temporary file */
save "`temp4'"      /* save memory into the temporary file */	

append using "`temp1'" 
append using "`temp2'" 
append using "`temp3'" 

sort value parmseq 

*rename variables
rename estimate mean_offer
rename stderr se_offer

gen hi_offer = mean_offer + se_offer
gen lo_offer = mean_offer - se_offer
	
rename parmseq position_xaxis1

*create graph: v=70 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==4 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==4 & value==70, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "NK" 2 "SK" 3 "BK" 4 "CI", labsize(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	text(-7 1.5 "Buyer doesn't ", size(medium)) ///
	text(-9.25 1.5 "know seller's cost", size(medium)) ///
	text(-7 3.5 "Buyer knows", size(medium)) ///	
	text(-9.25 3.5 "seller's cost", size(medium)) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerbytreatvalue70.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerbytreatvalue70.tif", replace


*create graph: v=90 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==4 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==4 & value==90, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "NK" 2 "SK" 3 "BK" 4 "CI", labsize(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	text(-7 1.5 "Buyer doesn't ", size(medium)) ///
	text(-9.25 1.5 "know seller's cost", size(medium)) ///
	text(-7 3.5 "Buyer knows", size(medium)) ///	
	text(-9.25 3.5 "seller's cost", size(medium)) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerbytreatvalue90.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerbytreatvalue90.tif", replace



****************************************
***B3a. Buyer Offer for SK vs CI (conditional on cost for CI) - V=70
***B3b. Buyer Offer for SK vs CI (conditional on cost for CI) - V=90
****************************************
use fairness_part1, replace
keep if buyer==1
keep if treatment == 2 | treatment == 4
keep if period>20
egen numeric_id=group(subj_id)	

gen treatment_id = . 
	replace treatment_id=1 if treatment==2
	replace treatment_id=2 if treatment==4 & othercost==10
	replace treatment_id=3 if treatment==4 & othercost==30

preserve
*save means with SE cluster
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=70
tempfile temp1    /* create a temporary file */
save "`temp1'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster for NK 
replace treatment_id=4 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==70, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==3
replace parmseq=1 if parmseq==3
gen value=70
tempfile temp2    /* create a temporary file */
save "`temp2'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen value=90
tempfile temp3    /* create a temporary file */
save "`temp3'"      /* save memory into the temporary file */	

restore
*save means with SE cluster for NK 
replace treatment_id=4 if treatment_id==1						 
parmby "reg offer i.treatment_id if value==90, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==3
replace parmseq=1 if parmseq==3
gen value=90
tempfile temp4    /* create a temporary file */
save "`temp4'"      /* save memory into the temporary file */	

append using "`temp1'" 
append using "`temp2'" 
append using "`temp3'" 

sort value parmseq 

*rename variables
rename estimate mean_offer
rename stderr se_offer

gen hi_offer = mean_offer + se_offer
gen lo_offer = mean_offer - se_offer
	
rename parmseq position_xaxis1

*create graph: v=70 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==70, barw(0.7) color(gs0) fintensity(inten0) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==70, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==70, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "SK" 2 "CI" 3 "CI", labsize(medium)) ///
	text(26.5 2 "(C=10)", size(medium)) ///
	text(26.5 3 "(C=30)", size(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerSKvCI_value70.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerSKvCI_value70.tif", replace


*create graph: v=90 only
local inv_golden_ratio = 1.5
graph set window fontface "Garamond" 
graph twoway (bar mean_offer position_xaxis1 if position_xaxis1==1 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==2 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(bar mean_offer position_xaxis1 if position_xaxis1==3 & value==90, barw(0.7) color(gs0) fintensity(inten75) lwidth(medthin)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==1 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==2 & value==90, color(gs6)) ///
	(rcap hi_offer lo_offer position_xaxis1 if position_xaxis1==3 & value==90, color(gs6)), ///
	ylabel(30(5)55, format(%9.0f) angle(black) labsize(medium)) ///
	ytitle("Average Offer", size(large)) ///
	xtitle("Treatment", size(large) height(14)) ///
	xlabel(1 "SK" 2 "CI" 3 "CI", labsize(medium)) ///
	text(26.5 2 "(C=10)", size(medium)) ///
	text(26.5 3 "(C=30)", size(medium)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 

graph export "$out_dir_gph/Fairness_offerSKvCI_value90.png", width(3900) replace
graph export "$out_dir_gph/Fairness_offerSKvCI_value90.tif", replace


****************************************
***B4. CDF of Offers Used to Elicit Beliefs
****************************************
use fairness_part2, replace

sort subj_id

gen probvalue=probBDM/100
gen probactualvalue=.
	replace probactualvalue=1 if oldvalue==70
	replace probactualvalue=0 if oldvalue==90	
	
****************************************
*** NK
****************************************

		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==1 & oldcost==10, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_NK10.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_NK10.tif", replace

		
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==1 & oldcost==30, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_NK30.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_NK30.tif", replace

		
****************************************
*** SK
****************************************		
	
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==2 & oldcost==10, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_SK10.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_SK10.tif", replace
		
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==2 & oldcost==30, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_SK30.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_SK30.tif", replace
		
****************************************
*** BK
****************************************		
	
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==3 & oldcost==10, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_BK10.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_BK10.tif", replace
		
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==3 & oldcost==30, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_BK30.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_BK30.tif", replace
		
****************************************
*** CI
****************************************		
	
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==4 & oldcost==10, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5)) 
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_CI10.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_CI10.tif", replace
		
		local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
		graph set window fontface "Garamond" 
		cdfplot oldoffer if treatment==4 & oldcost==30, by(oldvalue) ///
			opt1( lc(black gs6) lp(dash solid) ) ///
		ytitle("CDF", size(large)) ///
		xline(20, lcolor(gs9) lpattern(shortdash)) ///
		xline(60, lcolor(gs9) lpattern(shortdash)) ///
		xtitle("Offer", size(large)) ///
		xlabel(0(20)80, labsize(large)) ///
		ylabel(0(0.2)1, angle(black)) ///
		ylabel(, nogrid angle(horizontal) labsize(large)) ///
		aspectratio(`inv_golden_ratio') ///
		yscale(nofextend) xscale(nofextend) ///
		graphregion(fcolor(white) lcolor(white)) ///
		legend(label(1 "Value=70") label(2 "Value=90") ///
				order(1 2) position(10) ring(0) col(1) size(medium) symysize(*.5) symxsize(*.5))  
		
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_CI30.png", width(3900) replace
		graph export "$out_dir_gph/Fairness_cdf_part2randoffers_CI30.tif", replace
		

****************************************
***B5. Seller Acceptance Rate for BK vs NK conditional on seller's cost - ALL 30 ROUNDS
****************************************
use fairness_part1, replace
keep if buyer==0
keep if treatment == 1 | treatment == 3
egen numeric_id=group(subj_id)	
	
*create offer bins (by 10 units)	
gen offerbin_id=.
	replace offerbin_id=1 if offer>=10 & offer<=19
	replace offerbin_id=2 if offer>=20 & offer<=29
	replace offerbin_id=3 if offer>=30 & offer<=39
	replace offerbin_id=4 if offer>=40 & offer<=49
	replace offerbin_id=5 if offer>=50 & offer<=60
	

preserve
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==10 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen cost=10
gen treatment=1
tempfile temp1    /* create a temporary file */
save "`temp1'"      /* save memory into the temporary file */	

restore, preserve 
***TREATMENT==1 (NK)
*save means with SE cluster 
replace offerbin_id=6 if offerbin_id==1						 
parmby "reg accept i.offerbin_id if cost==10 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==5
replace parmseq=1 if parmseq==5
gen cost=10
gen treatment=1
tempfile temp2    /* create a temporary file */
save "`temp2'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==30 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen cost=30
gen treatment=1
tempfile temp3    /* create a temporary file */
save "`temp3'"      /* save memory into the temporary file */	

restore, preserve
*save means with SE cluster for NK 
replace offerbin_id=6 if offerbin_id==1						 
parmby "reg accept i.offerbin_id if cost==30 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==5
replace parmseq=1 if parmseq==5
gen cost=30
gen treatment=1
tempfile temp4    /* create a temporary file */
save "`temp4'"      /* save memory into the temporary file */

***TREATMENT==3 (BK)
restore, preserve
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==10 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen cost=10
gen treatment=3
tempfile temp5    /* create a temporary file */
save "`temp5'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster for BK
replace offerbin_id=6 if offerbin_id==1						 
parmby "reg accept i.offerbin_id if cost==10 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==5
replace parmseq=1 if parmseq==5
gen cost=10
gen treatment=3
tempfile temp6    /* create a temporary file */
save "`temp6'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==30 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
*only offerbins 3-5 available, adjust parmseq to align results 
replace parmseq = 4 if parmseq==2
replace parmseq = 5 if parmseq==3
gen cost=30
gen treatment=3
tempfile temp7    /* create a temporary file */
save "`temp7'"      /* save memory into the temporary file */	

restore
*save means with SE cluster for NK 
replace offerbin_id=6 if offerbin_id==3						 
parmby "reg accept i.offerbin_id if cost==30 & treatment==3, robust cluster(numeric_id) noconstant", norestore

*parmseq needed ==3 because only offer bins 3-5 available
keep if parmseq==3
replace parmseq=3 if parmseq==3
gen cost=30
gen treatment=3
tempfile temp8    /* create a temporary file */
save "`temp8'"      /* save memory into the temporary file */	

append using "`temp1'" 
append using "`temp2'" 
append using "`temp3'"
append using "`temp4'" 
append using "`temp5'" 
append using "`temp6'" 
append using "`temp7'" 
 
sort treatment cost parmseq 

*rename variables
rename estimate mean_accept
rename stderr se_accept

gen hi_accept = mean_accept + se_accept
gen lo_accept = mean_accept - se_accept
	
rename parmseq offerbin	
	
				
local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond" 
graph twoway (line mean_accept offerbin if treatment==1 & cost==10, color(gs8)) ///
         (line mean_accept offerbin if treatment==3 & cost==10, color(gs8) lpattern(dash) lwidth(medthick)) ///
	     (line mean_accept offerbin if treatment==1 & cost==30 & offerbin>=3, color(gs0)) ///
         (line mean_accept offerbin if treatment==3 & cost==30 & offerbin>=3, color(gs0) lpattern(dash) lwidth(medthick)) ///
	     (rcap hi_accept lo_accept offerbin if treatment==1 & cost==10, color(gs8)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==3 & cost==10, color(gs8)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==1 & cost==30 & offerbin>=3, color(gs0)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==3 & cost==30 & offerbin>=3, color(gs0)), ///
	ytitle("Seller's Acceptance Rate", size(large)) ///
	xtitle("Buyer's Offer", size(large)) ///
	xlabel(1 "10-19" 2 "20-29" 3 "30-39" 4 "40-49" 5 "50-60", labsize(medium) angle(90)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(label(1 "NK, Cost=10") label(2 "BK Cost=10") label(3 "NK, Cost=30") label(4 "BK Cost=30") ///
				order(1 3 2 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 				

graph export "$out_dir_gph/Fairness_acceptNKvsBK.png", width(3900) replace
graph export "$out_dir_gph/Fairness_acceptNKvsBK.tif", replace


****************************************
***B6. Seller Acceptance Rate for BK vs NK conditional on seller's cost - LAST ROUNDS
****************************************
use fairness_part1, replace
keep if period>20
keep if buyer==0
keep if treatment == 1 | treatment == 3
egen numeric_id=group(subj_id)	
	
*create offer bins (by 10 units)	
gen offerbin_id=.
	replace offerbin_id=1 if offer>=10 & offer<=19
	replace offerbin_id=2 if offer>=20 & offer<=29
	replace offerbin_id=3 if offer>=30 & offer<=39
	replace offerbin_id=4 if offer>=40 & offer<=49
	replace offerbin_id=5 if offer>=50 & offer<=60
	

preserve
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==10 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen cost=10
gen treatment=1
tempfile temp1    /* create a temporary file */
save "`temp1'"      /* save memory into the temporary file */	

restore, preserve 
***TREATMENT==1 (NK)
*save means with SE cluster 
replace offerbin_id=6 if offerbin_id==1						 
parmby "reg accept i.offerbin_id if cost==10 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==5
replace parmseq=1 if parmseq==5
gen cost=10
gen treatment=1
tempfile temp2    /* create a temporary file */
save "`temp2'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==30 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
gen cost=30
gen treatment=1
tempfile temp3    /* create a temporary file */
save "`temp3'"      /* save memory into the temporary file */	

restore, preserve
*save means with SE cluster for NK 
replace offerbin_id=6 if offerbin_id==1						 
parmby "reg accept i.offerbin_id if cost==30 & treatment==1, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==5
replace parmseq=1 if parmseq==5
gen cost=30
gen treatment=1
tempfile temp4    /* create a temporary file */
save "`temp4'"      /* save memory into the temporary file */

***TREATMENT==3 (BK)
restore, preserve
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==10 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
*only offerbins 2-5 available, adjust parmseq to align results 
replace parmseq = parmseq + 1
gen cost=10
gen treatment=3
tempfile temp5    /* create a temporary file */
save "`temp5'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster for BK
replace offerbin_id=6 if offerbin_id==2					 
parmby "reg accept i.offerbin_id if cost==10 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq==4
replace parmseq=2 if parmseq==4
gen cost=10
gen treatment=3
tempfile temp6    /* create a temporary file */
save "`temp6'"      /* save memory into the temporary file */	

restore, preserve 
*save means with SE cluster
parmby "reg accept i.offerbin_id if cost==30 & treatment==3, robust cluster(numeric_id) noconstant", norestore

keep if parmseq>1
*only offerbins 3-5 available, adjust parmseq to align results 
replace parmseq = 4 if parmseq==2
replace parmseq = 5 if parmseq==3
gen cost=30
gen treatment=3
tempfile temp7    /* create a temporary file */
save "`temp7'"      /* save memory into the temporary file */	

restore
*save means with SE cluster for BK
replace offerbin_id=6 if offerbin_id==3						 
parmby "reg accept i.offerbin_id if cost==30 & treatment==3, robust cluster(numeric_id) noconstant", norestore

*parmseq needed ==3 because only offer bins 3-5 available
keep if parmseq==3
replace parmseq=3 if parmseq==3
gen cost=30
gen treatment=3
tempfile temp8    /* create a temporary file */
save "`temp8'"      /* save memory into the temporary file */	

append using "`temp1'" 
append using "`temp2'" 
append using "`temp3'"
append using "`temp4'" 
append using "`temp5'" 
append using "`temp6'" 
append using "`temp7'" 
 
sort treatment cost parmseq 

*rename variables
rename estimate mean_accept
rename stderr se_accept

gen hi_accept = mean_accept + se_accept
gen lo_accept = mean_accept - se_accept
	
rename parmseq offerbin	
	
				
local inv_golden_ratio = 2 / ( sqrt(5) + 1 )
graph set window fontface "Garamond" 
graph twoway (line mean_accept offerbin if treatment==1 & cost==10, color(gs8)) ///
         (line mean_accept offerbin if treatment==3 & cost==10, color(gs8) lpattern(dash) lwidth(medthick)) ///
	     (line mean_accept offerbin if treatment==1 & cost==30 & offerbin>=3, color(gs0)) ///
         (line mean_accept offerbin if treatment==3 & cost==30 & offerbin>=3, color(gs0) lpattern(dash) lwidth(medthick)) ///
	     (rcap hi_accept lo_accept offerbin if treatment==1 & cost==10, color(gs8)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==3 & cost==10, color(gs8)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==1 & cost==30 & offerbin>=3, color(gs0)) ///
		 (rcap hi_accept lo_accept offerbin if treatment==3 & cost==30 & offerbin>=3, color(gs0)), ///
	ytitle("Seller's Acceptance Rate", size(large)) ///
	xtitle("Buyer's Offer", size(large)) ///
	xlabel(1 "10-19" 2 "20-29" 3 "30-39" 4 "40-49" 5 "50-60", labsize(medium) angle(90)) ///
	ylabel(, nogrid angle(horizontal) ) ///
	aspectratio(`inv_golden_ratio') ///
	yscale( nofextend ) xscale(nofextend) ///
	graphregion(fcolor(white) lcolor(white)) ///
	legend(label(1 "NK, Cost=10") label(2 "BK Cost=10") label(3 "NK, Cost=30") label(4 "BK Cost=30") ///
				order(1 3 2 4) position(6) ring(1) col(2) size(medium) symysize(*.5) symxsize(*.5)) 					

graph export "$out_dir_gph/Fairness_acceptNKvsBK_last10only.png", width(3900) replace
graph export "$out_dir_gph/Fairness_acceptNKvsBK_last10only.tif", replace


*END CODE
